package com.aizu.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.aizu.utils.HttpUtils;
import com.alibaba.fastjson.JSON;
import com.isp.encrypt.MD5;

@Service("eRentService")
public class ERentService {

	private Logger log = Logger.getLogger(this.getClass());

	public static final List<String> MD5_FIELDS = new ArrayList<String>();

	static {
		// MD5_FIELDS.add("phone");
		// MD5_FIELDS.add("twoCard");
		// MD5_FIELDS.add("oldPhone");
	}

	@Value("${eRent.basePath}")
	private String eRentPath;

	public Map<String, String> md5Fields(Map<String, Object> data) {
		Map<String, String> target = new HashMap<String, String>();
		Iterator<String> keyset = data.keySet().iterator();
		String key = null;
		while (keyset.hasNext()) {
			key = keyset.next();
			if (MD5_FIELDS.contains(key)) {
				try {
					target.put(key, MD5.encode(data.get(key).toString()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				target.put(key, data.get(key).toString());
			}
		}
		return target;
	}

	private Map<String, String> parseJsonAndReturnResult(String jsonStr) {
		// 读取JSON数据
		try {
			Map<String, Map<String, String>> json = (Map<String, Map<String, String>>) JSON.parse(jsonStr);
			return (Map<String, String>) json.get("result");
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, String> errorJson = new HashMap<String, String>();
			errorJson.put("flag", "1");
			errorJson.put("msg", "请稍后再试：" + jsonStr);
			return errorJson;
		}
	}

	/**
	 * 房主注册
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxDRegister(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRegister/wxDRegister", md5Fields(data));
		log.info("接口房主注册结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 增加房间
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxAddRoomClass(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxAddRoomClass", md5Fields(data));
		log.info("接口增加房间结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 修改房间
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxUpdRoomClass(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxUpdRoomClass", md5Fields(data));
		log.info("接口修改房间结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 删除房间
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxDelRoomClass(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxDelRoomClass", md5Fields(data));
		log.info("接口删除房间结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 新增床位
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxAddClassBed(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxAddClassBed", md5Fields(data));
		log.info("接口新增床位结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 修改床位
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxUpdClassBed(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxUpdClassBed", md5Fields(data));
		log.info("接口修改床位结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 删除床位
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxDelClassBed(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxDelClassBed", md5Fields(data));
		log.info("接口删除床位结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 绑定房客
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxAddBedGrant(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxAddBedGrant", md5Fields(data));
		log.info("接口绑定房客结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 解绑房客
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxDelBedGrant(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxDelBedGrant", md5Fields(data));
		log.info("接口解绑房客结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 新增人员
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxAddGrant(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxAddGrant", md5Fields(data));
		log.info("接口新增人员结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 编辑人员
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxUpdGrant(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxUpdGrant", md5Fields(data));
		log.info("接口编辑人员结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 删除人员
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxDelGrant(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRoomMain/wxDelGrant", md5Fields(data));
		log.info("接口删除人员结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

	/**
	 * 取消关注房客
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public Map<String, String> wxKCancel(Map<String, Object> data) throws IOException {
		String jsonStr = HttpUtils.post(eRentPath + "wxRegister/wxKCancel", md5Fields(data));
		log.info("接口取消关注房客结果：" + jsonStr);
		return parseJsonAndReturnResult(jsonStr);
	}

}
